﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Gx.Model.Other;
using Gx.Bll.Manage;
using Gx.Model.Entity;

namespace Gx.Bll.OtherBll
{
    public class TreeViewBll
    {
        private readonly E_DepartmentInfoBll depBll = new E_DepartmentInfoBll();
        private readonly E_LevelInfoBll levelBll = new E_LevelInfoBll();
        private readonly E_DepLevelInfoBll deplevelBll = new E_DepLevelInfoBll();

        /// <summary>
        /// 得到岗位管理  BasicManage/LevelList
        /// </summary>
        /// <param name="List"></param>
        /// <returns></returns>
        public List<TreeView> GetDepLevelJson()
        {
            List<TreeView> tvList = new List<TreeView>();
            List<E_DepartmentInfo> depList = depBll.GetDepList();
            List<E_DepLevelInfo> deplevelList = deplevelBll.GetE_DepLevelInfoList();
            List<E_DepartmentInfo> oneList = depList.Where(x => x.ParentId == 1).ToList();
            tvList.Add(new TreeView() { id = 1, text = "总公司", children = new List<TreeView>() });
            TreeView tv = null;
            foreach (var item in oneList)
            {
                tv = new TreeView()
                {
                    id = item.Id,
                    text = item.DepName,
                    children = new List<TreeView>(),
                };
                GetDepLevelForeach(tv, depList, deplevelList, item.Id);
                GetDepLevelForeach(tv, deplevelList, item.Id);
                tvList[0].children.Add(tv);
            }
            return tvList;
        }

        private void GetDepLevelForeach(TreeView tv, List<E_DepartmentInfo> depList, List<E_DepLevelInfo> depLevelList, int depId)
        {
            if (depList.Exists(x => x.ParentId == depId))
            {
                List<E_DepartmentInfo> _DepList = depList.Where(x => x.ParentId == depId).ToList();
                TreeView tvChild = null;
                foreach (var dep in _DepList)
                {
                    tvChild = new TreeView()
                    {
                        id = dep.Id,
                        text = dep.DepName,
                        children = new List<TreeView>(),
                    };
                    GetDepLevelForeach(tvChild, depList, depLevelList, dep.Id);
                    GetDepLevelForeach(tvChild, depLevelList, dep.Id);
                    tv.children.Add(tvChild);
                }
            }
        }

        private void GetDepLevelForeach(TreeView tv, List<E_DepLevelInfo> depLevelList, int depId)
        {
            if (depLevelList.Exists(x => x.DepId == depId))
            {
                List<E_DepLevelInfo> _depLevelList = depLevelList.Where(x => x.DepId == depId).ToList();
                foreach (var depLevel in _depLevelList)
                {
                    tv.children.Add(new TreeView()
                    {
                        id = depLevel.LevelId,
                        text = depLevel.LevelName,
                        attributes = new Attributes() { isLevel = true }
                    });
                }
            }
        }

        /// <summary>
        /// 得到树形部门  EmpBasicInfo/GetDepComBoTreeJson
        /// </summary>
        /// <param name="List"></param>
        /// <returns></returns>
        public List<TreeView> GetDepJson()
        {
            List<TreeView> tvList = new List<TreeView>();
            List<E_DepartmentInfo> depList = depBll.GetDepList();
            List<E_DepartmentInfo> oneList = depList.Where(x => x.ParentId == 0).ToList();
            TreeView tv = null;
            foreach (var item in oneList)
            {
                tv = new TreeView()
                {
                    id = item.Id,
                    text = item.DepName,
                    children = new List<TreeView>(),
                };
                GetDepComBoTreeForeach(tv, depList, item.Id);
                tvList.Add(tv);
            }
            return tvList;
        }

        private void GetDepComBoTreeForeach(TreeView tv, List<E_DepartmentInfo> depList, int depId)
        {
            if (depList.Exists(x => x.ParentId == depId))
            {
                List<E_DepartmentInfo> _DepList = depList.Where(x => x.ParentId == depId).ToList();
                TreeView tvChild = null;
                foreach (var dep in _DepList)
                {
                    tvChild = new TreeView()
                    {
                        id = dep.Id,
                        text = dep.DepName,
                        children = new List<TreeView>(),
                    };
                    GetDepComBoTreeForeach(tvChild, depList, dep.Id);
                    tv.children.Add(tvChild);
                }
            }
        }

         
    }
}
